*************The mass flow of nitrogen fertilizers and GHG emissions State and National********************
****Illinois*****
use "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\Illinois GHG emissions.dta"

****Defining base equations****
**FN = Fertilizer nitrogen applied
**EF1, EF4, EF5, EFdecomposition = Emission factors
**FracGASF, FracLEACH = Fractional loss factors
**LT = Limestone applied

gen EdirectN2O = FN * EF1 * (44/28) * 273
gen EGASF = FN * Frac_gasF * EF4 * (44/28) * 273
gen ELEACH = FN * Frac_leach * EF5 * (44/28) * 273
gen Edecomposition = FN * EFdecomposition
gen Elimestone = FN * LT * 0.12 * (44/12)

***formatting*********
format FN %15.0f
format EdirectN2O %15.0f
format EGASF %15.0f
format ELEACH %15.0f
format Edecomposition %15.0f
format Elimestone %15.0f

****Defining Sensitivity Scenarios***
*A sensitivity analysis examines how variations in emission factors (EFs) and fractions (Frac_gasF, FracLEACH, etc.) affect emissions.
*We will vary EF1, EF4, EF5, EFdecomposition, FracGASF, and FracLEACH for two-point (low/high) scenarios
***Base (use your existing variables)***************** 
***Low scenario***found in 11.13 and 11.26 of IPCC 2019 except for EFdec and LT, not available so varied by 20%
gen EF1_low = 0.002
gen Frac_gasF_low = 0.03
gen EF4_low = 0.002
gen Frac_leach_low = 0.1
gen EF5_low = 0.0005
gen EFdec_low = EFdecomposition*0.8
gen LT_low = LT*0.8
***Computing emissions under sensitivity scenarios***
gen EdirectN2O_low = FN * EF1_low * (44/28) * 273
gen EGASF_low      = FN * Frac_gasF_low * EF4_low * (44/28) * 273
gen ELEACH_low     = FN * Frac_leach_low * EF5_low * (44/28) * 273
gen Edec_low       = FN * EFdec_low
gen Elime_low      = FN * LT_low * 0.12 * (44/12)

***High scenario****
gen EF1_high = 0.018
gen Frac_gasF_high = 0.3
gen EF4_high = 0.05
gen Frac_leach_high = 0.8
gen EF5_high = 0.025
gen EFdec_high = EFdecomposition*1.2
gen LT_high = LT*1.2
***Computing emissions under sensitivity scenarios***
gen EdirectN2O_high = FN * EF1_high * (44/28) * 273
gen EGASF_high      = FN * Frac_gasF_high * EF4_high * (44/28) * 273
gen ELEACH_high     = FN * Frac_leach_high * EF5_high * (44/28) * 273
gen Edec_high       = FN * EFdec_high
gen Elime_high      = FN * LT_high * 0.12 * (44/12)


***Comparing the results**
summarize EdirectN2O_low EdirectN2O EdirectN2O_high
summarize EGASF_low EGASF EGASF_high
summarize ELEACH_low ELEACH ELEACH_high
summarize EFdec_low EFdecomposition EFdec_high
summarize LT_low Elimestone LT_high

***Percentage change analysis***
gen change_EdirectN2O = ((EdirectN2O_high - EdirectN2O_low) / EdirectN2O) * 100
gen change_EGASF = ((EGASF_high - EGASF_low) / EGASF) * 100
gen change_ELEACH = ((ELEACH_high - ELEACH_low) / ELEACH) * 100
gen change_EFdecomposition = ((Edec_high - Edec_low) / Edecomposition) * 100
gen change_Elimestone = ((LT_high - LT_low) / Elimestone) * 100
summarize change_EdirectN2O change_EGASF change_ELEACH change_EFdecomposition change_Elimestone

***Boxplot of Emissions in different scenarios***
graph box EdirectN2O_low EdirectN2O EdirectN2O_high, title("Sensitivity Analysis: EdirectN2O")
graph box EGASF_low EGASF EGASF_high, title("Sensitivity Analysis: EGASF")
graph box ELEACH_low ELEACH ELEACH_high, title("Sensitivity Analysis: ELEACH")
graph box Edec_low Edecomposition Edec_high, title("Sensitivity Analysis: Edecomposition")
graph box LT_low Elimestone LT_high, title("Sensitivity Analysis: Elimestone")


graph bar change_EdirectN2O change_EGASF change_ELEACH change_EFdecomposition change_Elimestone, ///
title("Sensitivity Analysis Results") ytitle("Percentage Change") bar(1, color(blue))

****IOWA calculations****************************************************************************
use "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\Iowa GHG emissions.dta"
***repeat above***
save "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\Iowa GHG emissions.dta", replace

***************MINNESOTA CALCULATIONS************************

use "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\Minnesota GHG emissions.dta"
****repeat above***
save "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\Minnesota GHG emissions.dta", replace

********************************************National emissions calculations********************************************
use "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\National GHG emissions.dta"
***repeat above***
save "C:\Users\PTshabalala\OneDrive - Union of Concerned Scientists\Fertilizer\National GHG emissions.dta> "

format Edec_low %15.0f
format Elime_low %15.0f
format EdirectN2O_high %15.0f
format EGASF_high %15.0f
format ELEACH_high %15.0f
format Edec_high %15.0f
format Elime_high %15.0f